﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronBrokers.InformationBroker.ClientManagerService;
using System.Windows.Input;
using System.Windows;

namespace IronBrokers.InformationBroker.Model
{
    public class PriviledgesWindowViewModel : ViewModelBase
    {
        #region Public properties
        public User User { get; private set; }

        public String NewPriviledgeName { get; set; }

        private String selectedPriviledge = null;
        public String SelectedPriviledge
        {
            get
            {
                return selectedPriviledge;
            }
            set
            {
                selectedPriviledge = value;
                UpdateProperty("IsPriviledgeSelected");
            }
        }

        public Boolean IsPriviledgeSelected
        {
            get
            {
                return SelectedPriviledge != null;
            }
        } 

        #endregion

        #region Constructor
        public PriviledgesWindowViewModel(User user)
        {
            this.User = user;
            if (user.Priviledges == null)
                user.Priviledges = new string[] { };
        } 
        #endregion

        #region AddPriviledge
        public ICommand AddPriviledgeCommand
        {
            get
            {
                return new RelayCommand(param => AddPriviledge());
            }
        }

        public void AddPriviledge()
        {
            try
            {
                using (ClientManagerServiceClient svc = new ClientManagerServiceClient())
                {
                    List<String> priviledgeList = new List<string>(User.Priviledges);
                    priviledgeList.Add(NewPriviledgeName);
                    String[] priviledges = priviledgeList.ToArray();

                    Response response = svc.SetUserPriviledges(User.Name, priviledges);
                    if (response.Success)
                    {
                        User.Priviledges = priviledges;
                        NewPriviledgeName = null;
                        UpdateProperty("User");
                        UpdateProperty("NewPriviledgeName");
                    }
                    else
                    {
                        throw new Exception(response.ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred during updating user priviledges.\n" + ex.Message);
            }
        } 
        #endregion

        #region DeletePriviledge
        public ICommand DeletePriviledgeCommand
        {
            get
            {
                return new RelayCommand(param => DeletePriviledge());
            }

        }

        public void DeletePriviledge()
        {
            try
            {
                using (ClientManagerServiceClient svc = new ClientManagerServiceClient())
                {
                    List<String> priviledgeList = new List<string>(User.Priviledges);
                    priviledgeList.Remove(SelectedPriviledge);
                    String[] priviledges = priviledgeList.ToArray();

                    Response response = svc.SetUserPriviledges(User.Name, priviledges);
                    if (response.Success)
                    {
                        User.Priviledges = priviledges;
                        UpdateProperty("User");
                    }
                    else
                    {
                        throw new Exception(response.ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred during updating user priviledges.\n" + ex.Message);
            }
        } 
        #endregion
    }
}
